﻿#region License
// Copyright (c) 2009 Shun Chi Guo
// E-Mail:shuncy@gmail.com
// Blog:http://shuncy.cnblogs.com
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace shun.PAPLib.Response
{
    public class ResultNotificationResponse
    {
        /// <summary>
        /// 消息ID
        /// </summary>
        public string PushID { get; set; }
        /// <summary>
        /// 结果代码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 结果描述
        /// </summary>
        public string Desc { get; set; }

        public ResultNotificationResponse(string pushid, string code, string desc)
        {
            this.PushID = pushid;
            this.Code = code;
            this.Desc = desc;
        }

        /// <summary>
        /// 取得响应包的字符串
        /// </summary>
        /// <returns></returns>
        public string GetString()
        {
            MemoryStream ms = new MemoryStream();
            string rtn = "";
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = Encoding.UTF8;
                settings.Indent = false;
                XmlWriter xml = XmlWriter.Create(ms, settings);
                xml.WriteStartDocument();
                //添加申明信息
                xml.WriteDocType("pap", "-//WAPFORUM//DTD PAP 1.0//EN", "http://www.wapforum.org/DTD/wml_1.1.xml", null);
                //创建PAP节点
                xml.WriteStartElement("pap");
                //创建resultnotification-response节点
                xml.WriteStartElement("resultnotification-response");
                xml.WriteAttributeString("push-id", PushID);
                xml.WriteAttributeString("code", Code);
                xml.WriteAttributeString("desc", Desc);
                xml.WriteEndElement();
                //结束PAP节点
                xml.WriteEndElement();
                //结束文档
                xml.WriteEndDocument();

                xml.Close();
                rtn = Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                ms.Close();
                ms.Dispose();
            }
            return rtn;
        }

        /// <summary>
        /// 取得当前包的字节数组，默认UTF-8编码
        /// </summary>
        /// <returns></returns>
        public byte[] GetBytes()
        {
            return GetBytes(Encoding.UTF8);
        }
        /// <summary>
        /// 根据指定的编码取得当前包的字节数据
        /// </summary>
        /// <param name="enc">指定的编码</param>
        /// <returns></returns>
        public byte[] GetBytes(Encoding enc)
        {
            return enc.GetBytes(GetString());
        }

    }
}
